﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Configuration;

using Newtonsoft.Json;

using Whoever.Patterns.MVC.Models;

using Comswing.Website.Models.BitcoinViewModels;
using Comswing.Website.Infrastructure;


namespace Comswing.Website.Controllers
{
    public class BitcoinController : Controller
    {
        public ActionResult GetState()
        {
            return Json(new AJAXResponse { Data = GlobalState.BitcoinState, Success = true });
        }

        public WeightledPrices WeightedPrices()
        {
            var json = WeightedPricesJSON().Content.ToString();
            var result = JsonConvert.DeserializeObject<WeightledPrices>(json);
            return result;
        }

        public ContentResult WeightedPricesJSON()
        {
            using (var webClient = new WebClient())
            {
                var url = ConfigurationManager.AppSettings["WeightledPricesURL"].ToString();

                var json = webClient.DownloadString(url);
                return new ContentResult { Content = json, ContentType = "application/json" };
            }
        }

        public ContentResult MarketsJSON()
        {
            using (var webClient = new WebClient())
            {
                var url = ConfigurationManager.AppSettings["MarketsURL"].ToString();

                var json = webClient.DownloadString(url);

                return new ContentResult { Content = json, ContentType = "application/json" };
            }
        }
    }
}
